﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace OurCrawler
{
   
    public partial class Main : Form
    {
        private int count;
        Thread[] threads;
        Thread threadParse;
        
        private void ThreadRun()
        {
            //dequeue uri
            //parse uri
            for(int i = 0; i<10000; i++)
            {
                
                list_lview.Items[int.Parse(Thread.CurrentThread.Name)].SubItems[1].Text = i.ToString() ;  
            }
        }
        private void RunParser()
        {
            //enque uri
        }
        private void Start()
        {
            threadParse = new Thread(new ThreadStart(RunParser));
            threadParse.Start();
            CreateThreads();
        }
        private void CreateThreads()
        {
            threads = new Thread[ count ];
            string[] subItems = { "", "" };

            for (int j = 0; j < count; j++)
            {
                list_lview.Items.Add( j.ToString( ) );
                list_lview.Items[j].SubItems.AddRange( subItems );
            }
            Monitor.Enter(list_lview);
            for (int i = 0; i < count; i++)
            {

                //list_lview.Items[i].SubItems[1].Text = i.ToString();
                threads[i] = new Thread(new ThreadStart( ThreadRun ));
                threads[i].Name = i.ToString( );
                threads[i].Start();

            }
            Monitor.Exit( list_lview );
           

        }

        public Main()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            start_btn.Enabled = false;
            stop_btn.Enabled = false;
            url_tbox.Enabled = false;
            resume_btn.Enabled = false;
        }

        private void start_btn_Click(object sender, EventArgs e)
        {
            start_btn.Enabled = false;
            url_tbox.Enabled = false;
            save_btn.Enabled = false;
            threadsCount_tbox.Enabled = false;
            stop_btn.Enabled = true;
            start_btn.Enabled = false;
            save_btn.Enabled = false;
            Start();

            
           
        }

        private void save_btn_Click(object sender, EventArgs e)
        {
            list_lview.Items.Clear();
            start_btn.Enabled = true;
            url_tbox.Enabled = true;
            resume_btn.Enabled = false;
            count = 0;
            try
            {
                if ( Convert.ToInt32( threadsCount_tbox.Text ) > 100 ) threadsCount_tbox.Text = 100.ToString();
                count = Convert.ToInt32(threadsCount_tbox.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show( ex.Message );
            }
            
            
            
        }

        private void stop_btn_Click(object sender, EventArgs e)
        {

            
            url_tbox.Enabled = true;
            save_btn.Enabled = true;
            clear_btn.Enabled = true;
            threadsCount_tbox.Enabled = true;
            resume_btn.Enabled = true;
            stop_btn.Enabled = false;
            for (int i = 0; i < count; i++)
            {
                if( threads[i].ThreadState == ThreadState.Running )
                threads[i].Suspend();
            }

        }

        private void resume_btn_Click(object sender, EventArgs e)
        {
            resume_btn.Enabled = false;
            clear_btn.Enabled = false;
            stop_btn.Enabled = true;
            save_btn.Enabled = false;
            for (int i = 0; i < count; i++)
            {
                if(threads[i].ThreadState == ThreadState.Suspended)
                threads[i].Resume();
            }
        }

        private void clear_btn_Click(object sender, EventArgs e)
        {
            list_lview.Items.Clear();
            resume_btn.Enabled = false;
        }

    }
}
